package aps

import org.json.JSONObject

/**
 * 重新录入物料校验返回
 * 已存在物料校验
 * 开始作业返回
 * 实时消息
 */
class SHOWMESSAGEController {

    def topicMsgThreadService

    def index() {

        def max = topicMsgThreadService.max
        def number = params.number ?: "5"
        number = Integer.parseInt(number)

        def EQPID = params.EQPID ?: ''

        LinkedList linkList = topicMsgThreadService.newMsgMap?.get(EQPID)
        LinkedList alertList = topicMsgThreadService.alertMap?.get(EQPID)

        if (!alertList) {
            alertList = LinkedList.newInstance()
            topicMsgThreadService.alertMap.put(EQPID, alertList)
        }

        def result = []
        if (linkList) {
            if (number >= max || number > linkList.size()) {
                result = linkList
            } else {
                def lastIndex = linkList.size()
                result = linkList.getAt((lastIndex - number..lastIndex - 1))
            }

            for (JSONObject data : result) {

//                println "${data.getClass()}值:${data}"
                def msgId = data?.msgId

                if (!alertList.contains(msgId)) {
                    alertList.addLast(msgId)
                } else {
                    data.remove("ISALERT")
                    data.put("ISALERT", false)
                }

                if (alertList.size() > number) {
                    alertList.removeFirst()
                }
            }
        }
//        println "pda将打印消息：${result}"
        render result?.toString()
    }
}